#include <iostream>
#include <vector>
#include <string>
#include <map>

using namespace std;

int getNum(char c){
    if(c == 'I'){ return 1; }
    if(c == 'V'){ return 5; }
    if(c == 'X'){ return 10; }
    if(c == 'L'){ return 50; }
    if(c == 'C'){ return 100; }
    if(c == 'D'){ return 500; }
    if(c == 'M'){ return 1000; }
    return 0;
}

int romanToInt(string s) {
    // map<char, int> mp = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
    int ret = 0;
    for(int i = 0; i < s.size(); i++){
        int value = getNum(s[i]);
        if(i+1 == s.size() || getNum(s[i+1]) <= value){
            ret += value;
        }else{
            ret += getNum(s[++i]) - value;
        }
    }
    return ret;
}

int mapRomanToInt(string s) {
    map<char, int> mp = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
    int ret = 0;
    for(int i = 0; i < s.size(); i++){
        int value = mp.find(s[i])->second;
        if(i+1 == s.size() || mp.find(s[i+1])->second <= value){
            ret += value;
        }else{
            ret += mp.find(s[++i])->second - value;
        }
    }
    return ret;
}

int main(){
    string s = "MCMXCIV";
    cout << romanToInt(s) << endl;
    return 0;
}
